package com.rabbiter.pm.controller;

import okhttp3.*;

import java.io.IOException;

public class BaiduOCR {
    private static final String API_KEY = "3Zzz1Dw20QMPCcpPxjN7aPF7";
    private static final String SECRET_KEY = "0wNj3VF3GnD8DcpyTEKbS6NLCogrrEmj";
    private static final String AUTH_URL = "https://aip.baidubce.com/oauth/2.0/token";

    public static String getAccessToken() throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
                .add("grant_type", "client_credentials")
                .add("client_id", API_KEY)
                .add("client_secret", SECRET_KEY)
                .build();

        Request request = new Request.Builder()
                .url(AUTH_URL)
                .post(body)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String responseBody = response.body().string();
            // 解析返回的JSON，获取access_token
            return responseBody.split("\"access_token\":\"")[1].split("\"")[0];
        } else {
            throw new IOException("获取AccessToken失败：" + response.body().string());
        }
    }


    public static void main(String[] args) {
        try {
            String token = getAccessToken();
            System.out.println("token==" + token);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}